﻿
#region 程序集引用区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;
using SubSonic.Query;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;

//引用实体层
using WebeTerm.Model;
using WebeTerm.Common;
#endregion

namespace WebeTerm.DbProvider{
    /// <summary>
    /// 文 件 名: CustomerInfoDAL.cs
    /// CLR版本 : v2.0.50727
    /// 作   者 : lijun.hu
    /// 版    权: Copyright @2011 lijun.hu(FORCEN HU) 
    /// 电子邮箱：lijun.hu@shinsoft.net
    /// 说    明: 客户信息
    /// 生成日期: 2014年12月5日
    /// </summary>
    public partial class CustomerInfoDAL:BaseDAL< CustomerInfo >{
        
        #region 通用查询
        /// <summary>
        /// 查询系统设置
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="pages">分页信息</param>
        /// <returns>系统设置</returns>
        public List< CustomerInfo > SearchCustomerInfo(CustomerInfoQuery queryParam, Pages pages)
        {
            IQueryable< CustomerInfo > iQuery = QueryCustomerInfo(queryParam);
            var items = base.SplitPage(iQuery, pages).ToList();
            items.ForEach(l=>{
                { if (l is IModel) { (l as IModel).SetIsLoaded(); } }    
            });
            return items;
        }

        /// <summary>
        /// 计算系统设置数
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <returns>系统设置数</returns>
        public int SearchCustomerInfoCount(CustomerInfoQuery queryParam)
        {
            return QueryCustomerInfo(queryParam).Count();
        } 
        #endregion

        #region 组合查询
        /// <summary>
        /// 设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <returns>客户信息</returns>
        public IQueryable< CustomerInfo > QueryCustomerInfo(CustomerInfoQuery queryParam)
        {
            IQueryable<CustomerInfo> iQuery = All();
            iQuery = QueryCustomerInfoById(queryParam, iQuery);
            iQuery = QueryCustomerInfoByCustomerName(queryParam, iQuery);
            iQuery = QueryCustomerInfoByCustomerEName(queryParam, iQuery);
            iQuery = QueryCustomerInfoByCustomerIdNo(queryParam, iQuery);
            iQuery = QueryCustomerInfoByCustomerTelephone(queryParam, iQuery);
            iQuery = QueryCustomerInfoByEmail(queryParam, iQuery);
            iQuery = QueryCustomerInfoByQQ(queryParam, iQuery);
            iQuery = QueryCustomerInfoByWeiXinNo(queryParam, iQuery);
            iQuery = QueryCustomerInfoByEmployeeId(queryParam, iQuery);
            iQuery = QueryCustomerInfoByCompanyId(queryParam, iQuery);
            return iQuery;
        }
        #endregion
        
        #region 单条件查询
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoById(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (queryParam.Id != Guid.Empty)
                iQuery = iQuery.Where(iq => iq.Id == queryParam.Id);
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByCustomerName(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.CustomerName))
                iQuery = iQuery.Where(iq => iq.CustomerName.Contains(queryParam.CustomerName.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByCustomerEName(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.CustomerEName))
                iQuery = iQuery.Where(iq => iq.CustomerEName.Contains(queryParam.CustomerEName.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByCustomerIdNo(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.CustomerIdNo))
                iQuery = iQuery.Where(iq => iq.CustomerIdNo.Contains(queryParam.CustomerIdNo.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByCustomerTelephone(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.CustomerTelephone))
                iQuery = iQuery.Where(iq => iq.CustomerTelephone.Contains(queryParam.CustomerTelephone.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByEmail(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.Email))
                iQuery = iQuery.Where(iq => iq.Email.Contains(queryParam.Email.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByQQ(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.QQ))
                iQuery = iQuery.Where(iq => iq.QQ.Contains(queryParam.QQ.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByWeiXinNo(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (!string.IsNullOrEmpty(queryParam.WeiXinNo))
                iQuery = iQuery.Where(iq => iq.WeiXinNo.Contains(queryParam.WeiXinNo.Trim()));
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByEmployeeId(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (queryParam.EmployeeId != null)
                iQuery = iQuery.Where(iq => iq.EmployeeId == queryParam.EmployeeId);
            return iQuery;
        }
        /// <summary>
        /// 根据设置 客户信息 查询
        /// </summary>
        /// <param name="queryParam">条件</param>
        /// <param name="iQuery">客户信息 查询</param>
        /// <returns>客户信息 查询</returns>
        protected virtual IQueryable< CustomerInfo > QueryCustomerInfoByCompanyId(CustomerInfoQuery queryParam, IQueryable< CustomerInfo > iQuery)
        {
            if (queryParam.CompanyId != null)
                iQuery = iQuery.Where(iq => iq.CompanyId == queryParam.CompanyId);
            return iQuery;
        }
        #endregion
    }
}    